package com.unlcn.ils.erpInterfaces.service.buss.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.unlcn.ils.erpInterfaces.dao.mapper.SmmodelsmapMapper;
import com.unlcn.ils.erpInterfaces.dao.model.Smmodelsmap;
import com.unlcn.ils.erpInterfaces.dao.model.Smstyle;
import com.unlcn.ils.erpInterfaces.dao.mapper.SmstyleMapper;
import com.unlcn.ils.erpInterfaces.service.buss.ISmstyleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * SMSTYLE 货物型号  服务实现类
 * </p>
 *
 * @author liangpeng
 * @since 2018-06-08
 */
@Service
public class SmstyleServiceImpl extends ServiceImpl<SmstyleMapper, Smstyle> implements ISmstyleService {

    @Autowired
    private SmmodelsmapMapper smmodelsmapMapper;

    @Autowired
    private SmstyleMapper smstyleMapper;

    @Override
    public Long getStyleId(String vehiclename, Long contractId) {

        // 查询 erp 中是否存在车型
        Wrapper<Smstyle> smstyleWrapper = new EntityWrapper<>();
        smstyleWrapper.eq("VCSTYLENAME", vehiclename);
        smstyleWrapper.eq("ICONTRACTID", contractId);
        smstyleWrapper.eq("ACTIVE", 1);
        List<Smstyle> smstyleList = smstyleMapper.selectList(smstyleWrapper);

        if (CollectionUtils.isEmpty(smstyleList)) {

            Long ilineid = smstyleMapper.getNextIlineid();
            //在erp中新增型号
            Smstyle smstyle = new Smstyle();
            smstyle.setIlineid(ilineid);
            smstyle.setIcontractid(contractId);
            smstyle.setVcstylename(vehiclename);
            smstyle.setItypeid(1L);
            smstyle.setVcunit("辆");
            smstyle.setVcuserno("system");

            //将车型信息存入到erp中
            smstyleMapper.insert(smstyle);

            return ilineid;
        } else {
            return smstyleList.get(0).getIlineid();
        }
    }
}
